package com.zxy.controller;

import com.aimao.model.OrderIng;
import com.aimao.model.OrderOk;
import com.zxy.feignclients.NacosAClient;
import com.zxy.feignclients.OrderServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//@RestController
@RefreshScope
public class TestController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private NacosAClient nacosAClient;
    @Autowired
    private OrderServiceClient orderServiceClient;
    @GetMapping("/test")
    public String callNacosA(){
//        如果RestTemplate 这个份Bean 有@LoadBalanced,RestTemplate 在执行的时候，就会增加一个拦截器
//        这个拦截器，把一级目录作为服务名，去Nacos找出对应的ip和端口，192.168.14.32：8080 192.168.14.32：9090
        String forObject = restTemplate.getForObject("http://nacos-a/port", String.class);
        return   forObject;
    }

    @GetMapping("/openfeign")
    public String callFeignNacosA(){
        String port = nacosAClient.getPort();
        return port;
    }
    @GetMapping("/calluser")
    public String calluser(@RequestParam Integer id){
        String userId = nacosAClient.getUserId(id);
        return userId;
    }
    @GetMapping("/callsleep")
    public String callsleep(@RequestParam Integer s){
        String body = nacosAClient.getsleep(s);
        return body;
    }
    @GetMapping("/callordering")
    public OrderOk callordering(){
        OrderIng orderIng = new OrderIng();
        orderIng.setUserId(66);
        return orderServiceClient.order(orderIng);
    }
}
